<template>
    <div class="home">
        <header>
            {{Name}}
            <el-button type="primary" @click="shopLogin=true" v-show="showBtn">登录</el-button>
            <el-button type="primary" @click="exit" v-show="showBtn==false">退出登录</el-button>
            <span class="el-icon-shopping-cart-full" style="font-size:30px" @click="gotoShopCar"></span>
        </header>
        <p class="bread">首页/{{$route.meta.title}}</p>
        <router-view></router-view>

        <!-- 登录弹出框 -->
        <el-dialog title="登录" :visible.sync="shopLogin">
            <el-form :model="form">
                <el-form-item label="账号">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.pwd"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="shopLogin = false">取 消</el-button>
                <el-button type="primary" @click="loginSuc">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            shopLogin: false,
            form: {
                name: "",
                pwd: "",
            },
            Name: localStorage.getItem("name") || "",
            showBtn:true
        };
    },
    watch:{
        showBtn:{
            handler(newVal){
                if(localStorage.getItem('name')){
                    this.showBtn=false
                }else{
                    this.showBtn=true
                }
            },
            immediate:true
        }
    },
    methods: {
        exit() {
            localStorage.removeItem("name");
            this.Name = "";
            this.showBtn=true
        },
        loginSuc() {
            //本地存储
            localStorage.setItem("name", this.form.name);
            this.Name = this.form.name;
            this.shopLogin = false; //登录框隐藏
            this.showBtn=false  //按钮隐藏
        },
        gotoShopCar() {
            this.$router.push("/shopcar").catch(err=>{})
        },
    },
};
</script>

<style lang="scss">
@media screen and (max-width: 600px) {
    .home {
        header {
            height: 10vh!important;
            line-height: 10vh!important;
            text-align: center;
        }
        .aside {
            p {
                padding: 3vh !important;
            }
        }
    }
}
.home {
    width: 100%;
    header {
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
    .aside {
        p {
            padding: 10px;
        }
        .red {
            border-left: 1px solid red;
            transform: scale(1.1);
        }
    }
    .main {
        width: 100%;
        height: 71vh;
        display: flex;
        flex-wrap: wrap;
        overflow: auto;

    }
}
</style>
